home *** CD-ROM | disk | FTP | other *** search
/ Stone Design / Stone Design.iso / Stone_Friends / Wave / WavesWorld / Source / Shaders / TexturingAndModeling:AProceduralApproach / KMShaders / KMCyclone.sl < prev    next >
Encoding:
Text File  |  1995-03-22  |  3.6 KB  |  115 lines

  1. /* modified by wave to KMCyclone for name space protection... */
  2. /*
  3.  * cyclone.sl - surface for a semi-opaque cloud layer to be put on an
  4.  *              earth-like planetary model to model clouds and a cyclone.
  5.  *
  6.  * DESCRIPTION:
  7.  *      When put on a sphere, sets the color & opacity of the sphere to
  8.  *   make it look like the clouds surrounding an Earth-like planet, with
  9.  *   a big cyclone.
  10.  *      The shader works by creating a fractal turbulence function over
  11.  *   the surface, then modulating the opacity based on this function in
  12.  *   a way that looks like clouds on a planetary scale.
  13.  *
  14.  *
  15.  * PARAMETERS:
  16.  *    Ka, Kd - the usual meaning
  17.  *    cloudcolor - the color of the clouds, usually white
  18.  *    max_radius
  19.  *    twist - controls the twisting of the clouds due to the cyclone.
  20.  *    offset, scale - control the linear scaling of the cloud value.
  21.  *    omega, octaves - controls the fractal characteristics of the clouds
  22.  *
  23.  *
  24.  * HINTS:
  25.  *    See the "planetclouds" shader for hints which apply equally well
  26.  *    to this shader.
  27.  *
  28.  *
  29.  * AUTHOR: Ken Musgrave
  30.  *    Conversion to Shading Language and other minor changes by Larry Gritz.
  31.  *
  32.  * REFERENCES:
  33.  *    _Texturing and Modeling: A Procedural Approach_, by David S. Ebert, ed.,
  34.  *    F. Kenton Musgrave, Darwyn Peachey, Ken Perlin, and Steven Worley.
  35.  *    Academic Press, 1994.  ISBN 0-12-228760-6.
  36.  *
  37.  * HISTORY:
  38.  *    ???? - original texture developed by Ken Musgrave.
  39.  *    Feb 1994 - Conversion to Shading Language by L. Gritz
  40.  *
  41.  * last modified 1 March 1994 by lg
  42.  */
  43.  
  44.  
  45.  
  46. #define TWOPI (2*PI)
  47.  
  48. /* Use signed Perlin noise */
  49. #define snoise(x) ((2*noise(x))-1)
  50. #define DNoise(p) (2*(point noise(p)) - point(1,1,1))
  51. #define VLNoise(Pt,scale) (snoise(DNoise(Pt)+(scale*Pt)))
  52. #define VERY_SMALL 0.001
  53.  
  54.  
  55.  
  56.  
  57. surface
  58. KMCyclone (float Ka = 0.5, Kd = 0.75;
  59.        float max_radius = 1;
  60.        float twist = 0.5;
  61.        float scale = .7, offset = .5;
  62.        float omega = 0.675;
  63.        float octaves = 4;)
  64. {
  65.   float radius, dist, angle, sine, cosine, eye_weight, value;
  66.   point Pt;                 /* Point in texture space */
  67.   point PN;                 /* Normalized vector in texture space */
  68.   point PP;                 /* Point after distortion */
  69.   float l, o, a, i;         /* Loop control for fractal sum */
  70.  
  71.   /* Transform to texture coordinates */
  72.   Pt = transform ("shader", P);
  73.  
  74.   /* Rotate hit point to "cyclone space" */
  75.   PN = normalize (Pt);
  76.   radius = sqrt (xcomp(PN)*xcomp(PN) + ycomp(PN)*ycomp(PN));
  77.  
  78.   if (radius < max_radius) {   /* inside of cyclone */
  79.       /* invert distance from center */
  80.       dist = pow (max_radius - radius, 3);
  81.       angle = PI + twist * TWOPI * (max_radius-dist) / max_radius;
  82.       sine = sin (angle);
  83.       cosine = cos (angle);
  84.       PP = point (xcomp(Pt)*cosine - ycomp(Pt)*sine,
  85.           xcomp(Pt)*sine + ycomp(Pt)*cosine,
  86.           zcomp(Pt));
  87.       /* Subtract out "eye" of storm */
  88.       if (radius < 0.05*max_radius) {  /* if in "eye" */
  89.       eye_weight = (.1*max_radius - radius) * 10;   /* normalize */
  90.       /* invert and make nonlinear */
  91.       eye_weight = pow (1 - eye_weight, 4);
  92.     }
  93.       else eye_weight = 1;
  94.     }
  95.   else PP = Pt;
  96.  
  97.   if (eye_weight > 0) {   /* if in "storm" area */
  98.       /* Compute VLfBm */
  99.       l = 1;  o = 1;  a = 0;
  100.       for (i = 0;  i < octaves  &&  o >= VERY_SMALL;  i += 1) {
  101.       a += o * VLNoise (PP * l, 1);
  102.       l *= 2;
  103.       o *= omega;
  104.     }
  105.       value = abs (eye_weight * (offset + scale * a));
  106.     }
  107.   else value = 0;
  108.  
  109.   /* Thin the density of the clouds */
  110.   Oi = value * Os;
  111.  
  112.   /* Shade like matte, but with color scaled by cloud opacity */
  113.   Ci = Oi * (Ka * ambient() + Kd * diffuse(faceforward(normalize(N),I)));
  114. }
  115.